***********************************************************************************
   
Stata ado-file to compute Gini index and Concentration (Pseudo-Gini) index   

***********************************************************************************
 

 cap program drop ginis 
 program define ginis 
  
  version 7.0
  syntax varname [if] [in] [aw pw fw/] [, sort(varname) id(varlist)]
*  local varlist "required existing max(1)"
*  local weight "aweight fweight"
*  local if "optional"
*  local in "optional"
*  local options "id(string) sort(string)"
*  parse "`*'"
  
  if "`sort'"=="" {local sortvarlist = "`1' `id'"} 
  	 else { local sortvarlist "`sort' `id'" } 
  
  
  quietly
  {
   preserve
	tempvar _use weivar
     	gen byte _use = 1 `if' `in'
   	quiet keep if _use==1
  
        if "`weight'"=="" { quiet generate `weivar'=1}
          else { quiet generate `weivar'=`exp' }
  
  	
        gsort  -"`sortvarlist'"

    	tempvar v1
    	quiet gen `v1' = max(`1', 0)
    	quiet summarize `v1' [aweight=`weivar']
    	tempname numind mean
    	scalar `numind'=_result(2)
    	scalar `mean'=_result(3)

    	tempvar rho newvar
    	quiet generate `rho'=`weivar'[_n-1]
    	quiet replace `rho'=1 in 1
    	quiet replace `rho'=sum(`rho')

    	generate `newvar'=`weivar'*`v1'*(`rho'+(`weivar'-1)/2)
    	quiet summarize `newvar'
    	global ginis=1+1/`numind'-2/(`mean'*`numind'^2)*_result(18)
    	/* Myles, STB-48 */
    	
    	if "`sort'"==""  { noisily disp in yellow "$ginis"}
    	   else { noisily disp in yellow "$ginis"}    	  
    	    
    restore
  }  
 
 end program

